package com.cqust.wdm.emsboot.controller;

import com.cqust.wdm.emsboot.config.security.ShiroUtils;
import com.cqust.wdm.emsboot.domain.Certificate;
import com.cqust.wdm.emsboot.domain.User;
import com.cqust.wdm.emsboot.service.ICertificateService;
import com.cqust.wdm.emsboot.util.AjaxResult;
import com.cqust.wdm.emsboot.util.BaseController;
import com.cqust.wdm.emsboot.util.ExcelUtil;
import com.cqust.wdm.emsboot.util.page.TableDataInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 入库单/出库单（凭证）Controller
 */

@RestController
@RequestMapping("/emsboot/certificate")
public class CertificateController extends BaseController {
    @Autowired
    private ICertificateService certificateService;

    /**
     * 查询入库单/出库单 列表
     */

    @GetMapping("/list")
    public TableDataInfo list(Certificate certificate) {
        User userInfo = ShiroUtils.getUserInfo();

        if (userInfo != null) {
            //普通用户查询 自己的入库单，出库单
            if ("普通用户".equals(userInfo.getRoleName()))
                certificate.setUid(Long.valueOf(userInfo.getId()));
        }
        startPage();
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        return getDataTable(list);
    }

    @GetMapping("/auditedList")
    public TableDataInfo auditedList(Certificate certificate) {
        User userInfo = ShiroUtils.getUserInfo();
        //设置审核状态为：已审核
        certificate.setAuditFlag(1L);
        if (userInfo != null) {
            //普通用户查询 自己的入库单，出库单
            if ("普通用户".equals(userInfo.getRoleName()))
                certificate.setUid(Long.valueOf(userInfo.getId()));
        }
        startPage();
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        return getDataTable(list);
    }

    /**
     * 导出入库单/出库单列表
     */

    @PostMapping("/export")
    public void export(HttpServletResponse response, Certificate certificate) {
        List<Certificate> list = certificateService.selectCertificateList(certificate);
        ExcelUtil<Certificate> util = new ExcelUtil<Certificate>(Certificate.class);
        util.exportExcel(response, list, "入库单/出库单（凭证）数据");
    }

    /**
     * 获取入库单/出库单 详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(certificateService.selectCertificateById(id));
    }
    @ApiOperation("新增入库单")
    /**
     * 新增入库单/出库单
     */
    @PostMapping
    public AjaxResult add(@RequestBody Certificate certificate) {
        //自动添加 uid
        User userInfo = ShiroUtils.getUserInfo();
        if (userInfo != null) {
            certificate.setUid(Long.valueOf(userInfo.getId()));
        }
        return toAjax(certificateService.insertCertificate(certificate));
    }

    /**
     * 修改入库单/出库单
     */

    @PutMapping
    public AjaxResult edit(@RequestBody Certificate certificate) {
        return toAjax(certificateService.updateCertificate(certificate));
    }

    /**
     * 删除入库单/出库单
     */

    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(certificateService.deleteCertificateByIds(ids));
    }
}
